package com.yangli.bilibili.zuo;

import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.util.PriorityQueue;

/**
 * @Description 喝咖啡问题：arr[i]表示咖啡机冲咖啡时间，洗咖啡杯的时间a，咖啡杯不洗自然变干净时间b,n表示多少人喝咖啡.
 */
public class DrinkingCoffee {
    public static void main(String[] args) throws ParseException {


        DrinkingCoffee drinkingCoffee = new DrinkingCoffee();

        System.out.println(drinkingCoffee.forceSolution(new int[]{5, 1, 3}, 2, 10, 5));

    }

    //表示喝咖啡的开始和持续时间，这样就比较那个咖啡机最快可用
    private static class WrapClass implements Comparable<WrapClass> {
        int startTime;
        int periodTime;

        WrapClass(int startTime, int periodTime) {
            this.startTime = startTime;
            this.periodTime = periodTime;
        }

        @Override
        public int compareTo(@NotNull DrinkingCoffee.WrapClass o) {
            return (this.startTime + this.periodTime) - (o.startTime + o.periodTime);
        }
    }

    public int forceSolution(int[] arr, int a, int b, int n) {
        //边界控制省略
        PriorityQueue<WrapClass> priorityQueue = new PriorityQueue<WrapClass>();
        for (int i = 0; i < arr.length; ++i) {
            priorityQueue.add(new WrapClass(0, arr[i]));
        }
        int[] drinks = new int[n];
        for (int i = 0; i < n; ++i) {
            WrapClass wrapClass = priorityQueue.poll();
            wrapClass.startTime = wrapClass.startTime + wrapClass.periodTime;
            drinks[i] = wrapClass.startTime;
            priorityQueue.add(wrapClass);
        }

        return forceProcess(drinks, a, b, 0, 0);
    }

    public int forceProcess(int[] drinks, int a, int b, int index, int available) {
        if (index == drinks.length - 1) {
            return Math.min(Math.max(drinks[index], available) + a, drinks[index] + b);
        }
        //使用a方案
        int wash = Math.max(drinks[index], available) + a;  // 洗完我这一杯，时间来到哪？
        int next1 = forceProcess(drinks, a, b, index + 1, wash);
        int p1 = Math.max(wash, next1);

        int dry = drinks[index] + b;
        int next2 = forceProcess(drinks, a, b, index + 1, dry);
        int p2 = Math.max(dry, next2);
        return Math.min(p1, p2);

    }


}
